package com.taplinker.client.im.adapter;

import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.taplinker.client.AppConfigFactory;
import com.taplinker.client.R;
import com.taplinker.client.core.ImageManager;
import com.taplinker.client.db.bean.Conversation;
import com.taplinker.client.im.domain.EnumMessageType;
import com.taplinker.client.im.protocol.TapLinkerConversation;
import com.taplinker.client.im.rpc.C103Cmd;
import com.taplinker.client.im.rpc.Cmd012;
import com.taplinker.client.im.util.SmileyParser;
import com.taplinker.core.util.TimeUtil;
import com.winona.ConstantValue;

public class QueueListAdapter extends BaseAdapter {
	private List<TapLinkerConversation> conversationList;
	private Context context;
	private SmileyParser parser;
	private TapLinkerConversation conversation = new TapLinkerConversation();

	public QueueListAdapter(Context context,
			List<TapLinkerConversation> conversationList) {
		super();
		this.context = context;
		this.conversationList = conversationList;
		this.parser = new SmileyParser(context);
	}

	@Override
	public int getCount() {
		return conversationList.size();
	}

	@Override
	public Object getItem(int position) {
		return conversationList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.queue_list_item, null);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.content = (TextView) convertView.findViewById(R.id.message);
			holder.time = (TextView) convertView.findViewById(R.id.time);
			holder.head = (ImageView) convertView.findViewById(R.id.image);
			holder.count = (TextView) convertView.findViewById(R.id.unread);
			holder.delete = convertView.findViewById(R.id.delete);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.name.setText(AppConfigFactory.getInstance().getUserInfo()
				.getUserName());
		if (!TextUtils.isEmpty(AppConfigFactory.getInstance().getUserInfo()
				.getUserIcon())) {
			ImageManager.getInstance().loadImage(
					ConstantValue.IMAGE
							+ AppConfigFactory.getInstance().getExpert()
									.getHead(), holder.head, 60, 60, null);
		}

		final TapLinkerConversation conversation = conversationList
				.get(position);
		setVisitor(holder, conversation);
		setUnreadMsg(holder, conversation);
		setHead(holder, conversation);
		setLastMsg(holder, conversation);
		setTime(holder, conversation);
		holder.delete
				.setOnClickListener(new OnClickDelete(conversation.getId()));
		System.out.println(((TextView) holder.delete).getText() + "我被点击了");
		return convertView;
	}

	private void setHead(ViewHolder holder, TapLinkerConversation conversation) {

		String url = conversation.getPictures();
		ImageManager.getInstance().loadImage(url, holder.head, 60, 60, null);

	}

	private void setVisitor(final ViewHolder holder,
			final TapLinkerConversation conversation) {
		String name = conversation.getVisitor();
		if (!TextUtils.isEmpty(conversation.getCustomerId())) {
			name = conversation.getCustomerName();
		}
		if (name.length() > 8) {
			name = name.substring(name.lastIndexOf('_') + 1);
		}
		holder.name.setText(name);
	}

	private void setUnreadMsg(final ViewHolder holder,
			final TapLinkerConversation conversation) {
		if (conversation.getUnreadMessages() > 0) {
			holder.count.setVisibility(View.VISIBLE);
			holder.count.setText(String.valueOf(conversation
					.getUnreadMessages()));
		} else {
			holder.count.setVisibility(View.GONE);
		}
	}

	private void setLastMsg(final ViewHolder holder,
			final TapLinkerConversation conversation) {
		if (!TextUtils.isEmpty(conversation.getLastMessage())) {
			if (EnumMessageType.TEXT.getType().equals(
					conversation.getMessageType())) {
				CharSequence cs = parser.replace(conversation.getLastMessage());
				holder.content.setText(cs != null ? cs.toString() : "");
			} else if (EnumMessageType.IMAGE.getType().equals(
					conversation.getMessageType())) {
				holder.content.setText("<图片>");
			} else if (EnumMessageType.AUDIO.getType().equals(
					conversation.getMessageType())) {
				holder.content.setText("<语音>");
			}
		} else {
			holder.content.setText("");
		}
	}

	private void setTime(final ViewHolder holder,
			final TapLinkerConversation conversation) {
		if (conversation.getUpdateDate() != null) {
			holder.time.setText(TimeUtil.timeFormat(conversation
					.getUpdateDate()));
		} else {
			holder.time.setText("");
		}
	}

	public List<TapLinkerConversation> getData() {
		return conversationList;
	}

	public void setData(List<TapLinkerConversation> data) {
		conversationList = data;
	}

	private final class OnClickDelete implements View.OnClickListener {

		private String id ;

		public OnClickDelete(String id) {
			super();
			this.id = id;
		}

		@Override
		public void onClick(View v) {
			new Cmd012(id, "", context).execute();
			
		}

	}

	class ViewHolder {
		public TextView name;
		public TextView content;
		public TextView time;
		public TextView count;
		public ImageView head;
		View delete;
	}

}
